﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace UI.Admin
{
    public partial class nhanvien : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }
        void HienThiDanhSach()
        {
            // Get All Sản Phẩm
            DataTable Manufacturers = DBConnection.getALL("sp_getAllNhanVien");
            GridView1.DataSource = Manufacturers;
            GridView1.DataBind();

            DataTable dtNhasx = DBConnection.getALL("sp_getAllQuyen");
            txtMaQuyen.DataSource = dtNhasx;
            txtMaQuyen.DataValueField = "MaQuyen";
            txtMaQuyen.DataTextField = "TenQuyen";
            txtMaQuyen.DataBind();

            MultiView1.ActiveViewIndex = 0;
            btninsert.Enabled = false;
        }


        void HienThiKetQuaSua(String manxb)
        {


            SqlParameter[] thamso = new SqlParameter[]
            {
                 new SqlParameter("@MaNV",manxb)
            };
            DataTable dtsp = DBConnection.GetDatatableByStoreProcedureName("sp_getNhanVienbyMaNV", thamso);
            lbmaNV.Text = dtsp.Rows[0]["MaNV"].ToString();
            txtcmnd.Text = dtsp.Rows[0]["CMND"].ToString();
            txtdiachi.Text = dtsp.Rows[0]["DiaChi"].ToString();
            txtemail.Text = dtsp.Rows[0]["Email"].ToString();
            if (dtsp.Rows[0]["GioiTinh"].ToString() == "True")
            {
                txtgioitinh.SelectedIndex = 1;
            }
            else
                txtgioitinh.SelectedIndex = 0;
            txthoten.Text = dtsp.Rows[0]["HoTen"].ToString();
            txtMaQuyen.Text = dtsp.Rows[0]["MaQuyen"].ToString();
            txtngaysinh.Text = dtsp.Rows[0]["NgaySinh"].ToString();
            txtngayvaolam.Text = dtsp.Rows[0]["NgayVaoLam"].ToString();
            txtpassword.Text = dtsp.Rows[0]["Password"].ToString();
            txtSDT.Text = dtsp.Rows[0]["SDT"].ToString();
            txtusername.Text = dtsp.Rows[0]["UserName"].ToString();
            MultiView1.ActiveViewIndex = 1;
        }

        protected void btntaomoi_Click(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            btninsert.Visible = true;
            MultiView1.ActiveViewIndex = 1;
            btninsert.Enabled = true;
            lbmaNV.Text = "Mã tự động tăng";

        }


        protected void btntmkiem_Click(object sender, EventArgs e)
        {

            //string tennxb = txttim.Text;
            //SqlParameter[] thamso = new SqlParameter[]{
            //new SqlParameter("p_tennhaxb",tennxb)
            //};

            //// search NhaXB by TenNhaXB
            //DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchNhaXuatBanByTenNhaXB", thamso);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //txttim.Text = "";
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //GridView1.PageIndex = e.NewPageIndex;
            //this.HienThiDanhSach();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;

            string masp = GridView1.SelectedRow.Cells[2].Text;
            this.HienThiKetQuaSua(masp);
        }
        protected void btnback_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }


        protected void btnupdate_Click(object sender, EventArgs e)
        {   
            SqlParameter[] thamso = new SqlParameter[]
            {
            new SqlParameter("@MaNV",int.Parse(lbmaNV.Text)),
            new SqlParameter("@MaQuyen",txtMaQuyen.SelectedValue),
            new SqlParameter("@UserName",txtusername.Text),
            new SqlParameter("@Password",txtpassword.Text),
            new SqlParameter("@Email",txtemail.Text),
            new SqlParameter("@CMND",txtcmnd.Text),
            new SqlParameter("@HoTen",txthoten.Text),
            new SqlParameter("@NgaySinh",DateTime.Parse(txtngaysinh.Text)),
            new SqlParameter("@GioiTinh",txtgioitinh.SelectedIndex),
            new SqlParameter("@SDT",txtSDT.Text),
            new SqlParameter("@DiaChi",txtdiachi.Text),
            new SqlParameter("@NgayVaoLam",DateTime.Parse(txtngayvaolam.Text)),
             };

            DBConnection.QueryByStoreProcedureName("sp_updateNhanVien", thamso);
            this.HienThiDanhSach();
        }


        protected void btninsert_Click(object sender, EventArgs e)
        {

            //tao ma nha xuat ban tu dong tang = lay max manhaxb+1 
            string pass = "XJxZJ8kCSkQ=";
            DataTable dt = DBConnection.getALL("sp_countNhanVien");
            int maNV = int.Parse(dt.Rows[0]["soluong"].ToString()) + 2;
            SqlParameter[] thamso = new SqlParameter[]
            {
                new SqlParameter("@MaNV",maNV),
                new SqlParameter("@MaQuyen",txtMaQuyen.SelectedValue),    
                new SqlParameter("@UserName",txtusername.Text),
                new SqlParameter("@Password",pass),
                new SqlParameter("@Email",txtemail.Text),
                new SqlParameter("@CMND",txtcmnd.Text),    
                new SqlParameter("@HoTen",txthoten.Text),
                new SqlParameter("@NgaySinh",DateTime.Parse( txtngaysinh.Text)),
                new SqlParameter("@GioiTinh",txtgioitinh.SelectedIndex),
                new SqlParameter("@SDT",txtSDT.Text),    
                new SqlParameter("@DiaChi",txtdiachi.Text),
                new SqlParameter("@NgayVaoLam",DateTime.Parse(txtngayvaolam.Text)),
             };
            DBConnection.QueryByStoreProcedureName("insertNhanVien", thamso);
            this.HienThiDanhSach();
        }


        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string maNSX = GridView1.Rows[e.RowIndex].Cells[2].Text;

            try
            {
                SqlParameter[] thamso = new SqlParameter[]
                 {
                new SqlParameter("@MaNV",int.Parse(maNSX)),
                 };
                DBConnection.QueryByStoreProcedureName("sp_deleteNhanVien", thamso);
            }
            catch
            {
                System.Console.WriteLine("khong xoa duoc do rang buoc khoa");
            }
            HienThiDanhSach();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
}